package repo

import (
	"time"
)

var cacheMap map[string]*cacheDevice

type cacheDevice struct {
	Dev
	lastHeartbeatTime time.Time
	online            bool
}

func initCache(devices []Dev) {
	cacheMap = make(map[string]*cacheDevice)
	for _, device := range devices {
		cacheMap[device.DeviceId] = &cacheDevice{
			Dev: device,
		}
	}
}

func getCache(deviceId string) (*cacheDevice, bool) {
	dev, ok := cacheMap[deviceId]
	return dev, ok
}

func upsertCache(dev Dev) {
	cacheMap[dev.DeviceId] = &cacheDevice{
		Dev: dev,
	}
}

func rmCache(deviceId string) {
	delete(cacheMap, deviceId)
}

func getAllCache() []*cacheDevice {
	arr := make([]*cacheDevice, 0)
	for _, v := range cacheMap {
		arr = append(arr, v)
	}
	return arr
}
